package com.zaofan.mobile.controller;

import com.zaofan.bean.Address;
import com.zaofan.exception.ServiceException;
import com.zaofan.service.AddressService;
import com.zaofan.service.HotelService;
import com.zaofan.utils.C3p0DataSourceUtils;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

import org.apache.commons.dbutils.QueryRunner;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/mobile/user/address")
public class AddressController extends BaseServlet {

	private static final long serialVersionUID = 5884947841079014459L;
	private AddressService addressService = new AddressService();
	private HotelService mHotelService = new HotelService();

	/**
	 * 用户地址列表接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String temp = request.getParameter("userId");
			int accountId = Integer.valueOf(temp);
			List<Address> addresses = addressService.findByAccountId(accountId);
			JsonUtils.writeSuccessJson("查询成功", addresses, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 用户 增加地址
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Address address = WebUtils.write2bean(request, Address.class);
			System.out.println(address.toString());
			addressService.addAddress(address);
            if (request.getSession().getAttribute("address") == null)
                request.getSession().setAttribute("address",address);
			JsonUtils.writeSuccessJson("添加成功", "", response);
			return;
		} catch (ServiceException service) {
			JsonUtils.writeFailJson(service.getMessage(), response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("添加失败", response);
		}
		JsonUtils.writeFailJson("添加失败!!" + ",未知错误", response);
	}

	/**
	 * 用户 删除地址
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Address address = WebUtils.write2bean(request, Address.class);
			addressService.delete(address);
			JsonUtils.writeSuccessJson("删除成功", "", response);
			return;
		} catch (ServiceException service) {
			JsonUtils.writeFailJson(service.getMessage(), response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("删除失败:" + e.getMessage(), response);
		}
		JsonUtils.writeFailJson("删除失败!!" + ",未知错误", response);
	}

	/**
	 * 用户 修改地址
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Address address = WebUtils.write2bean(request, Address.class);
			addressService.updateAddress(address);
			JsonUtils.writeSuccessJson("修改成功", "", response);
			return;
		} catch (ServiceException service) {
			JsonUtils.writeFailJson(service.getMessage(), response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("修改失败", response);
		}
		JsonUtils.writeFailJson("修改失败!!" + ",未知错误", response);
	}
	/**
	 * 用户 修改地址
	 *
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void setDefault(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        Connection conn = C3p0DataSourceUtils.getConnection();
        try {
			Address address = WebUtils.write2bean(request, Address.class);
            conn.setAutoCommit(false);
            QueryRunner qr = new QueryRunner();
            qr.update(conn,"update address set isDefault = 'n' where accountId = ?",new Object[]{address.getAccountId()});
            qr.update(conn,"update address set isDefault = 'y' where accountId = ? and id = ?",new Object[]{address.getAccountId(),address.getId()});
            conn.commit();
			JsonUtils.writeSuccessJson("修改成功", "", response);
			return;
		} catch (Exception e) {
            conn.rollback();
			e.printStackTrace();
			JsonUtils.writeFailJson("修改失败", response);
		}finally {
            conn.close();
        }
        JsonUtils.writeFailJson("修改失败!!" + ",未知错误", response);
	}

	protected void getDefault(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String temp = request.getParameter("userId");
			int accountId = Integer.valueOf(temp);
			  Address  address = addressService.findDefaultByAccountId(accountId);
			JsonUtils.writeSuccessJson("查询成功", address, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}


	protected void recentAddress(HttpServletRequest request,HttpServletResponse response){
		try {
			String temp = request.getParameter("userId");
			int accountId = Integer.valueOf(temp);
			Address addresses = addressService.findrecentAddress(accountId);
			JsonUtils.writeSuccessJson("查询成功", addresses, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}

	protected void createHotel(HttpServletRequest request,HttpServletResponse response){
		try {
			int builderId = Integer.parseInt(request.getParameter("builderId"));
			String hotelNo = request.getParameter("hotelNo");

			mHotelService.addHotel(hotelNo,builderId);

			JsonUtils.writeSuccessJson("新增成功", hotelNo, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("新增失败,已存在的酒店号", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);

	}


}
